package com.fjh.shop.logistics.service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjh.shop.common.PageResult;
import com.fjh.shop.common.Result;
import com.fjh.shop.logistics.model.entity.Logistics;
import com.fjh.shop.logistics.service.service.LogisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuyunbin
 * @since 2025-07-16
 */


@RestController
@RequestMapping("/logistics")
@Tag(name = "物流管理", description = "物流信息的CRUD操作")
public class LogisticsController {

    @Autowired
    private LogisticsService logisticsService;

    @PostMapping
    @Operation(summary = "新增物流信息", description = "创建一个新的物流信息")
    public Result<Logistics> addLogistics(@RequestBody Logistics logistics) {
        Logistics savedLogistics = logisticsService.addLogistics(logistics);
        return Result.success(savedLogistics);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除物流信息", description = "根据ID删除物流信息")
    public Result<Boolean> deleteLogistics(@PathVariable @Parameter(description = "物流信息ID") Long id) {
        boolean result = logisticsService.deleteLogistics(id);
        return Result.success(result);
    }

    @PutMapping
    @Operation(summary = "更新物流信息", description = "更新物流信息")
    public Result<Logistics> updateLogistics(@RequestBody Logistics logistics) {
        Logistics updatedLogistics = logisticsService.updateLogistics(logistics);
        return Result.success(updatedLogistics);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取物流详情", description = "根据ID获取物流详情")
    public Result<Logistics> getLogisticsById(@PathVariable @Parameter(description = "物流信息ID") Long id) {
        Logistics logistics = logisticsService.getLogisticsById(id);
        return Result.success(logistics);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询物流信息", description = "分页查询物流信息，支持多条件查询")
    public PageResult<Logistics> pageLogistics(
            @RequestParam(defaultValue = "1") @Parameter(description = "当前页码") Integer current,
            @RequestParam(defaultValue = "10") @Parameter(description = "每页记录数") Integer pageSize,
            @RequestParam(required = false) @Parameter(description = "物流单号") String logisticsNo,
            @RequestParam(required = false) @Parameter(description = "订单ID") Long orderId,
            @RequestParam(required = false) @Parameter(description = "订单编号") String orderNo,
            @RequestParam(required = false) @Parameter(description = "收件人姓名") String receiverName,
            @RequestParam(required = false) @Parameter(description = "收件人电话") String receiverPhone
    ) {
        IPage<Logistics> page = logisticsService.pageLogistics(
                current, pageSize, logisticsNo, orderId, orderNo, receiverName, receiverPhone);
        return PageResult.success(page);
    }
}
